package com.fishsticks.model;

import java.util.ArrayList;
import java.util.Date;

import android.test.AndroidTestCase;

import com.fishsticks.R;
import com.fishsticks.view.LoginActivity;
import com.fishsticks.view.RegisterActivity;
import com.fishsticks.view.ToDoListActivity;

import junit.framework.Assert;
import junit.framework.TestCase;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.test.ActivityInstrumentationTestCase2;


/**
 * this class tests the database manager class to see if its functionality is effective
 * @author Michael
 *
 */
public class DatabaseManagerTest extends AndroidTestCase {
	
	private DatabaseManager db;
	
	@Override
	protected void setUp() {
		db = new DatabaseManager(getContext());
		db.setLoggedInUser("default");
		ArrayList<ToDoItem> toDoList = db.getToDoItems("default");
		for(int i=0; i<toDoList.size(); i++) {
			db.deleteToDo(toDoList.get(i));
		}
	}
	
	@Override
	protected void tearDown() {
		ArrayList<ToDoItem> toDoList = db.getToDoItems("default");
		for(int i=0; i<toDoList.size(); i++) {
			db.deleteToDo(toDoList.get(i));
		}
		db.close();
	}

	
	/**tests that there are some valid credentials in the database
	 * 
	 */
	public void testCheckCredentials() {
		Assert.assertTrue(db.checkCredentials("default", "default"));
	}
	
	/**
	 * tests that there are no invalid users in then database
	 */
	public void testInvalidCheckCredentials() {
		Assert.assertFalse(db.checkCredentials("default", "agbpaoisdf"));
	}

	
	
	
	
	/**tests that a certain username does not exist
	 * 
	 */
	public void testInvalidCheckIfUsernameExists() {
		Assert.assertFalse(db.checkIfUsernameExists("claygoddardisdisappoint"));
	}

	/**tests that a certain username exists
	 * 
	 */
	public void testValidCheckIfUsernameExists() {
		Assert.assertTrue(db.checkIfUsernameExists("default"));
	}
	
	
	
	/**tests that adding an item to the todolist works
	 * 
	 */
	public void testAddToDoItem() {
		//db.setLoggedInUser("default");
		//Assert.assertTrue(db.checkCredentials("default", "default"));
		ArrayList<ToDoItem> list = db.getToDoItems("default");
		int sizeBefore = list.size();
		
		db.addToDoItem("do things", "grocery store", new Date(2013, 01, 01), "default", new Category("School"), "17.23423", "18.23487");
		
		list = db.getToDoItems("default");
		int sizeAfter = list.size();
		
		Assert.assertEquals(sizeBefore, sizeAfter - 1);
	}
	
	public void testInvalidAddToDoItem() {
		//db.setLoggedInUser("default");
		//Assert.assertTrue(db.checkCredentials("default", "default"));
		ArrayList<ToDoItem> list = db.getToDoItems("default");
		int sizeBefore = list.size();
		
		db.addToDoItem("", "", new Date(2013, 01, 01), "default", new Category("School"), "17.23423", "18.23487");
		
		list = db.getToDoItems("default");
		int sizeAfter = list.size();
		
		Assert.assertEquals(sizeBefore, sizeAfter);
	}

	
	
	
	/**tests the adding of a category
	 * 
	 */
	public void testAddCategory() {
		//Assert.assertTrue(db.checkIfUsernameExists("default"));
		db.setLoggedInUser("default");
		db.addCategory("FarmTasks");
		Assert.assertTrue(db.categoryExists(new Category("FarmTasks"), "default"));
	}
	
	/**tests the adding of a category
	 * 
	 */
	public void testInvalidAddCategory() {
		//Assert.assertTrue(db.checkIfUsernameExists("default"));
		db.setLoggedInUser("default");
		db.addCategory("");
		Assert.assertFalse(db.categoryExists(new Category(""), "default"));
	}

	
	
	
	/**tests that the logged in user is correct
	 * 
	 */
	public void testGetLoggedInUser() {
		Assert.assertEquals("default", db.getLoggedInUser());
	}
	
	/**tests that the logged in user is a faulty login
	 * 
	 */
	public void testNotLoggedInUser() {
		Assert.assertFalse("asdfas".equals(db.getLoggedInUser()));
	}
	
	
	
	
	
	/**test that the database manager deletes from the todolist properly
	 * 
	 */
	public void testDeleteToDo() {
		ArrayList<ToDoItem> list = db.getToDoItems("default");
		int sizeBefore = list.size();
		
		db.addToDoItem("do things", "grocery store", new Date(2013, 01, 01), "default", new Category("School"), "17.23423", "18.23487");
		ArrayList<ToDoItem> list2 = db.getToDoItems("default");
		db.deleteToDo(list2.get(0));
		ArrayList<ToDoItem> list3 = db.getToDoItems("default");
		list = db.getToDoItems("default");
		int sizeAfter = list3.size();
		
		Assert.assertEquals(sizeBefore, sizeAfter);
	}
	/**test that the database manager deletes from the todolist properly
	 * 
	 */
	public void testInvalidDeleteToDo() {
		ArrayList<ToDoItem> list = db.getToDoItems("default");
		int sizeBefore = list.size();
		ToDoItem todo = new ToDoItem(null, null, null, null, null, false, sizeBefore);
		db.addToDoItem("do things", "grocery store", new Date(2013, 01, 01), "default", new Category("School"), "17.23423", "18.23487");
		db.deleteToDo(todo);
		list = db.getToDoItems("default");
		int sizeAfter = list.size();
		Assert.assertEquals(sizeBefore, sizeAfter - 1);
	}

}
